<?php

if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}

class table_er_kami_buy_type extends discuz_table
{
    public function __construct()
    {
        $this->_table = 'er_kami_buy_type';
        $this->_pk = 'id';
        parent::__construct();
    }

    /**
     * 获取分类列表
     * @return array 分类列表
     */
    public function fetch_all($page = 1, $pagesize = 10, $query = []) {
        $where = array();
        $params = array($this->_table);

        if(!empty($query) && is_array($query)) {
            foreach($query as $key => $value) {
                $allowed_keys = ['id', 'name', 'extcredits', 'credit'];
                if(!in_array($key, $allowed_keys)) continue;
                if($value!== null && $value!== '' && $value!= 0) {
                    if(is_int($value) || ctype_digit($value)) {
                        $where[] = $key.'=%d';	
                    } else {
                        $where[] = $key.'=%s';	
                    }	
                    $params[] = $value;
                }
            }
        }

        $count_sql = "SELECT COUNT(*) FROM %t";
		if(!empty($where)) {
			$count_sql .= " WHERE " . implode(' AND ', $where);
		}
        $total = DB::result_first($count_sql, $params);

        $sql =  "SELECT t.*, COUNT(b.id) as kami_count FROM %t t 
                LEFT JOIN ".DB::table('er_kami_buy')." b ON t.id = b.kami_type AND b.status = 1 AND (b.expire_time > %d OR b.expire_time = 0)";

        if(!empty($where)) {
            $sql .= " WHERE " . implode(' AND ', $where);
        }

        $sql .= " GROUP BY t.id ORDER BY t.sort ASC";

        $start = ($page - 1) * $pagesize;
        $limit_sql = " LIMIT $start, $pagesize";
        $sql.= $limit_sql;
        
        $params[] = TIMESTAMP;
        $data = DB::fetch_all($sql, $params);

        return array(
            'data' => $data,
            'total' => $total
        );
    }

    /**
     * 批量插入数据
     * @param array $data 数据数组
     * @return bool 插入成功返回true，失败返回false
     */
    public function insert_batch($data) {
        if(empty($data)) return false;
        
        $sql = "INSERT INTO ".DB::table($this->_table)." (name, desc, extcredits, credit) VALUES ";
        $values = [];
        $params = [];

        foreach($data as $item) {
            $values[] = "(%s, %s, %d, %d)";
            $params[] = $item['name'];
            $params[] = $item['desc'];
            $params[] = $item['extcredits'];
            $params[] = $item['credit'];
        }

        $sql .= implode(',', $values);
        return DB::query($sql, $params);
    }

    /**
     * 更新数据
     * @param int $id 数据id
     * @param array $data 更新数据
     * @return bool 更新成功返回true，失败返回false
     */
    public function update($id, $data, $unbuffered = false, $low_priority = false) {
        return DB::update($this->_table, $data, array('id' => $id), $unbuffered, $low_priority);	
    }
    
    /**
     * 删除数据
     * @param int $id 数据id
     * @return bool 删除成功返回true，失败返回false
     */
    public function delete($id, $unbuffered = false) {
        return DB::delete($this->_table, array('id' => $id), $unbuffered);
    }

}